#-*- encoding: gb18030 -*-
import sys, string, os
import pygame
from config import *
from pygame.locals import *
    
class OTT(pygame.sprite.Sprite):
    def __init__(self):
        self.direct = 0
        self.index = 0
        self.count = 0
        left = self.__init_frame(True)
        right = self.__init_frame(False)
        self.subimg = [left, right, [left[1], right[1]], [left[1], right[1]]]
        
    def __init_frame(self, isleft):
        # generate head & leg
        font = pygame.font.Font(FONT, HEAD_SIZE)
        head = font.render(HEAD, 1, HEAD_COLOR)
        font = pygame.font.Font(FONT, LEG_SIZE)
        leg = font.render(LEG, 1, LEG_COLOR)
        legr = pygame.transform.rotate(leg, 30)
        legl = pygame.transform.flip(legr, 0, 1)
        # generate TT
        tt = [pygame.Surface((legr.get_width() * 2, legr.get_height()))] * 4
        tt[0].blit(legl, (0, 0))
        tt[0].blit(legr, (legl.get_width(), 0))
        tt[1].blit(leg, (0, 0))
        tt[1].blit(leg, (leg.get_width(), 0))
        tt[2].blit(legr, (0, 0))
        tt[2].blit(legl, (legr.get_width(), 0))
        tt[3].blit(leg, (0, 0))
        tt[3].blit(leg, (leg.get_width(), 0))
        # generate OTT or TTO
        frame = [pygame.Surface((head.get_width() + max(leg.get_width(), legr.get_width()), 

head.get_height()))] * 4
        if isleft:
            for i in range(4):
                frame[i].blit(head, (0, 0))
                frame[i].blit(tt[i], (head.get_width(), frame[i].get_height() - tt

[i].get_height()))
        else:
            for i in range(4):
                frame[i].blit(head, (tt[i].get_width(), 0))
                frame[i].blit(tt[i], (0, frame[i].get_height() - tt[i].get_height()))
        # return frame
        return frame        
            
    # left = 0, right = 1, up = 2, down = 3
    def update(self, direct, pos):
        self.count += 1
        if self.count >= OTT_UPDATE_SPEED:
            self.count = 0
            if direct == self.direct:
                self.index = (self.index + 1) % len(self.subimg[self.direct])
            elif direct == 2 or direct == 3:
                self.inde = self.direct
            else:
                self.direct = direct
                self.index = 0
        window.fill((0, 0, 0))
        window.blit(self.subimg[self.direct][self.index], pos)

pygame.init()
window = pygame.display.set_mode((GAME_WIDTH, GAME_HEIGHT))
pygame.display.set_caption(GAME_CAPTION)
x = 400
y = 300
clock = pygame.time.Clock()
pygame.key.set_repeat()
ott = OTT()
direct = 0

font = pygame.font.Font(FONT, HEAD_SIZE)
head = font.render(HEAD, 1, HEAD_COLOR)
font = pygame.font.Font(FONT, LEG_SIZE)
leg = font.render(LEG, 1, LEG_COLOR)
legr = pygame.transform.rotate(leg, 30)
legl = pygame.transform.rotate(leg, -30)

fleg = pygame.transform.flip(leg, 1, 0)
flegr = pygame.transform.flip(legr, 1, 0)
flegl = pygame.transform.flip(legl, 1, 0)
# generate TT
tt = []
for i in range(4):
    tt.append(pygame.Surface((legr.get_width() * 2, legr.get_height())))
tt[0].blit(legl, (0, 0))
tt[0].blit(flegl, (legl.get_width(), 0))
tt[1].blit(leg, (0, 0))
tt[1].blit(fleg, (leg.get_width(), 0))
tt[2].blit(legr, (0, 0))
tt[2].blit(flegr, (legr.get_width(), 0))
tt[3].blit(leg, (0, 0))
tt[3].blit(fleg, (leg.get_width(), 0))
# generate OTT or TTO
left = []
right = []
for i in range(4):
    left.append(pygame.Surface((head.get_width() + tt[i].get_width(), head.get_height())))
    left[i].blit(head, (0, 0))
    left[i].blit(tt[i], (head.get_width(), left[i].get_height() - tt[i].get_height()))
    right.append(pygame.transform.flip(left[i], 1, 0))# return frame
window.fill((0, 0, 0))
window.blit(tt[2], (x, y))
pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            pass 
"""
while True:
    clock.tick(30)
    if (x >= 800): x = 0
    if (y >= 600): y = 0
    
    pygame.display.update()
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            pass 

    kname = pygame.key.get_pressed()
    #print kname
    if kname[pygame.K_LEFT]:
        if x > 0: x = x - 3
        ott.update(0, (x, y))
    elif kname[pygame.K_RIGHT]:
        if x < 800: x = x + 3
        ott.update(1, (x, y))
    elif kname[pygame.K_UP]: 
        if y > 0: y = y - 3
        ott.update(2, (x, y))
    elif kname[pygame.K_DOWN]:
        if y < 600: y = y + 3
        ott.update(3, (x, y))
"""